package com.kintreda.ieco_server.module.api.web;

import com.kintreda.ieco_server.bean.article.Article;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.module.article.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.api.web
 * @Author ZhongYao_H
 * @Date 2019/5/14:16:40
 * @Year 2019 Month 05 Day 14
 * @Date_zh 2019 年 五月 星期二
 */
@Controller
@RequestMapping("/api/article")
public class ApiArticleController {


    @Autowired
    IBaseRepository<Article,String> articleStringIBaseRepository;
    @Autowired
    IArticleService articleService;

    @RequestMapping(value = "/details",method = RequestMethod.POST)
    @ResponseBody
    public Object details(String id){
        return articleService.get(id);
    }


    @RequestMapping(value = "/query",method = RequestMethod.POST)
    @ResponseBody
    public Object query(String type){
        List<Map<String, Object>> list = articleStringIBaseRepository.findListBySql("select id,title,anno,type,createDate,coverId from arti where type = ? order by createDate desc limit 0,50 ", new Object[]{type});
        return list.size()>0?list:null;

    }

    /**
     * 得到分类攻略
     *
     * @return
     */
    @RequestMapping(value = "/getstrategy",method = RequestMethod.POST)
    @ResponseBody
    public Object get(){
        List<Article> query = articleService.query("from Article where type = ? ", new Object[]{"2"});
        return query.size()>0?query.get(0):null;
    }

    /**
     *
     *
     * @return
     */
    @RequestMapping(value = "/get",method = RequestMethod.POST)
    @ResponseBody
    public Object get(String id){
        return articleService.get(id);
    }

    /**
     * 根据类型查询文章
     *
     * @return
     */
    @RequestMapping(value = "/getbytype",method = RequestMethod.POST)
    @ResponseBody
    public Object getByType(String type){
        List<Article> query = articleService.query("from Article where type = ? order by createDate desc", new Object[]{type});
        return query;
    }


}
